#!/bin/bash
## 声明所有代码均为本人所写
function make_repo() {
	nmcli con up ens33
	echo "正在配置YUM源"
        echo "-------------------"
	rm -rf /etc/yum.repos.d/*
	curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
	yum clean all
	yum makecache
        echo -e "-------------------\n"
}

function install_jdk() {
	echo "正在安装jdk"
        echo "-------------------"
	tar -zxvf ./jdk-8u411-linux-x64.tar.gz
	mkdir -p /usr/java
	cp -r ./jdk1.8.0_411 /usr/java/
	cat >> /etc/profile <<EOF
export JAVA_HOME=/usr/java/jdk1.8.0_411 
export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export PATH=$PATH:${JAVA_HOME}/bin
EOF
	source /etc/profile
	java -version
	echo -e "-------------------\n"
}

function install_oracle() {
	echo "正在安装oracle所需依赖"
	echo "-------------------"
	yum -y install binutils compat-libcap1 compat-libstdc++-33 compat-libstdc++-33*.i686 elfutils-libelf-devel gcc gcc-c++ glibc*.i686 glibc glibc-devel glibc-devel*.i686 ksh libgcc*.i686 libgcc libstdc++ libstdc++*.i686 libstdc++-devel libstdc++-devel*.i686 libaio libaio*.i686 libaio-devel libaio-devel*.i686 make sysstat unixODBC unixODBC*.i686 unixODBC-devel unixODBC-devel*.i686 libXp libXtst
	yum -y install vim tar net-tools wget perl python3 readline* deltarpm python-deltarpm \
 zip unzip bc compat-libcap1* compat-libcap* binutils compat-libstdc++-33 \
 elfutils-libelf elfutils-libelf-devel gcc gcc-c++ glibc-2.5 glibc-common glibc-devel \
 glibc-headers ksh libaio libaio-devel libgcc libstdc++ libstdc++-devel make sysstat \
 unixODBC unixODBC-devel binutils* compat-libstdc* elfutils-libelf* \
 gcc* glibc* ksh* libaio* libgcc* libstdc* make* sysstat* libXp* glibc-kernheaders\
 openssh-clients openssh-server;
	echo -e "-------------------\n"
}

function disable_security() {
	echo "正在关闭防火墙和selinux"
	echo "-------------------"
	systemctl disable --now firewalld.service
        sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
	setenforce 0
	echo -e "-------------------\n"
}

function create_user() {
	echo "正在创建oracle用户及用户组"
	echo "-------------------"
        groupadd oinstall
	groupadd dba
	groupadd oper
	useradd -g oinstall -G dba,oper oracle
	echo admin | passwd --stdin oracle
	echo "oracle用户密码为: admin"
	echo -e "-------------------\n"	
}


function create_dir() {
        echo "正在创建目录"
        echo "-------------------"
	mkdir -p /home/app/oracle/product/11.2.0/db_1
	chown -R oracle:oinstall /home/app
	chmod -R 775 /home/app
	
	mkdir -p /usr/oracle
	chown -R oracle:oinstall /usr/oracle 
	chmod -R 755 /usr/oracle/
	echo "请将oracle11g安装包手动移动到/usr/oracle目录下"
        echo -e "-------------------\n"
}

function make_sysctl() {
        echo "正在优化系统参数"
        echo "-------------------"
	cat >> /etc/sysctl.conf << EOF
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmall = 2097152
kernel.shmmax = 536870912
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
EOF
	sysctl -p
	cat >> /etc/pam.d/login << EOF
session required /lib64/security/pam_limits.so
session required pam_limits.so
EOF
	
	getconf LONG_BIT
	source /etc/profile
        echo -e "-------------------\n"
}

function makeEnv_oracle() {
        echo "正在配置oracle用户环境变量"
        echo "-------------------"
	cat >> /home/oracle/.bash_profile << EOF
export ORACLE_BASE=/home/app/oracle 
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
export PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_SID=orcl
export ORACLE_BASE ORACLE_HOME ORACLE_SID PATH
#NLS_LANG="SIMPLIFIED CHINESE_CHINA.UTF8"
export LANG=en_US
if [ $USER = "oracle" ]; then
   if [ $SHELL = "/bin/ksh" ]; then
       ulimit -p 16384
       ulimit -n 65536
   else
       ulimit -u 16384 -n 65536
   fi
fi
EOF
	source /home/oracle/.bash_profile
	if [ $? -eq 0 ];then
		echo "[OK]"
	else
		echo "[ERROR]"
		exit 6
	fi
        echo -e "-------------------\n"

}

function make_set() {
        echo "正在设置基础配置"
        echo "-------------------"
	echo "127.0.0.1 oracle" >> /etc/hosts
	ping -c 3 oracle
	echo "HOSTNAME=oracle" >> /etc/sysconfig/network
	yum install xorg-x11-xauth xterm -y
        echo -e "-------------------\n"

}

echo "请将oracle11g安装包手动移动到/usr/oracle目录下"

make_repo
install_jdk
install_oracle
disable_security
create_user
create_dir
make_sysctl
makeEnv_oracle
make_set
